package cluster

import (
	"context"
	"kms-backend/config"
	"kms-backend/utils/logs"
	"kms-backend/utils/tools"
	"net/http"

	"github.com/gin-gonic/gin"
	corev1 "k8s.io/api/core/v1"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// 新增和更新集群信息的功能相识，可以统一使用

func addOrUpdate(r *gin.Context, method string) {
	var flag string
	if method == "Create" {
		flag = "添加"
	} else {
		flag = "更新"
	}
	logs.Debug(nil, flag+"集群")
	returnData := config.NewReturnData()
	clusterConfig := ClusterConfing{}
	if err := r.ShouldBindJSON(&clusterConfig); err != nil {
		msg := flag + "集群失败，集群信息不完整" + err.Error()
		returnData.Message = msg
		returnData.Status = 400
		r.JSON(200, returnData)
		return
	}

	// 判断集群是否正常
	clusterStatus, err := clusterConfig.getClusterStatus()
	if err != nil {
		msg := "无法获取集群信息: " + err.Error()
		returnData.Message = msg
		returnData.Status = 400
		r.JSON(http.StatusOK, returnData)
		logs.Error(map[string]interface{}{"error": err.Error()}, flag+"集群失败,无法获取集群信息")
		return
	}

	logs.Info(map[string]interface{}{"集群名称": clusterConfig.DisplayName, "集群ID": clusterConfig.Id}, "开始"+flag+"集群")
	// 创建一个存储集群配置的secret
	var clusterConfigSecret corev1.Secret

	clusterConfigSecret.Name = clusterConfig.Id

	// labels
	clusterConfigSecret.Labels = make(map[string]string)
	clusterConfigSecret.Labels[config.ClusterConfingSecretKey] = config.ClusterConfingSecretValue

	// annotations, 添加注释，保存集群配置信息
	clusterConfigSecret.Annotations = make(map[string]string)
	// clusterConfigSecret.Annotations["displayName"] = clusterConfig.DisplayName
	// clusterConfigSecret.Annotations["city"] = clusterConfig.City
	// clusterConfigSecret.Annotations["district"] = clusterConfig.District
	// 优化以上代码
	// 把集群状态结构体转换成map
	m := tools.StructToMap(clusterStatus)
	clusterConfigSecret.Annotations = m
	// 保存kubeconfig 信息
	clusterConfigSecret.StringData = make(map[string]string)
	clusterConfigSecret.StringData["kubeconfig"] = clusterConfig.KubeConfig

	// 调用clientSet 创建Secret
	// config配置文件中定义一个clientSet类型的变量，controller 中init 方法中创建的clientSet赋值给该变量
	if method == "Create" {
		_, err = config.InClusterClientSet.CoreV1().Secrets(config.MetadataNamespace).Create(context.TODO(), &clusterConfigSecret, metav1.CreateOptions{})
	} else {
		_, err = config.InClusterClientSet.CoreV1().Secrets(config.MetadataNamespace).Update(context.TODO(), &clusterConfigSecret, metav1.UpdateOptions{})
	}

	if err != nil {
		logs.Error(map[string]interface{}{"集群ID": clusterConfig.Id, "集群名称": clusterConfig.DisplayName, "msg": err.Error()}, flag+"集群Secret失败")
		msg := flag + "集群失败" + err.Error()
		returnData.Message = msg
		returnData.Status = 400
		r.JSON(200, returnData)
		return
	}
	// 初始化中获取的集群信息中，加入新增的集群
	config.ClusterKubeConfig[clusterConfig.Id] = clusterConfig.KubeConfig
	// 测试输出config.ClusterKubeConfig的信息
	// fmt.Print(config.ClusterKubeConfig)
	logs.Info(map[string]interface{}{"集群ID": clusterConfig.Id, "集群名称": clusterConfig.DisplayName}, flag+"集群成功")
	returnData.Message = "集群" + flag + "成功"
	returnData.Status = 200
	r.JSON(200, returnData)

}
